package com.wyz.note.mq.rabbit.provider.controller;

import com.wyz.note.mq.rabbit.provider.constant.BaseConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description: 测试生产者推送数据(直连型交换机)
 * @Author: wei yz
 * @Date: 2022/6/12 14:16
 */
@RestController
public class DirectSendMessageController {
    private static final Logger log = LoggerFactory.getLogger(DirectSendMessageController.class);

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(String message) {
        String messageId = BaseConstant.messageId;
        String createTime = BaseConstant.createTime;
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", message);
        map.put("createTime", createTime);
        log.info("推送数据 ==> messageId: {},message: {},createTime: {}", messageId, message, createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(BaseConstant.DirectExchange, BaseConstant.DirectRoutingKey, map);
        return "ok";
    }
}
